package c.c.b.b.e;

import android.content.Context;
import android.text.TextUtils;
import c.b.a.a.a;
import c.c.b.b.c;
import c.c.b.b.g.b;
import java.util.HashMap;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;

/* compiled from: TNCConfigHandler */
public class e {
    public static final Object d = new Object();
    public Context a;
    public d b;
    public boolean c = true;

    public e(Context context, boolean z) {
        this.a = context;
        this.c = z;
        this.b = new d();
    }

    public void a() {
        if (this.c) {
            String string = this.a.getSharedPreferences("ttnet_tnc_config", 0).getString("tnc_config_str", null);
            String str = "TNCConfigHandler";
            if (TextUtils.isEmpty(string)) {
                b.a(str, "loadLocalConfig: no existed");
                return;
            }
            StringBuilder stringBuilder;
            try {
                d b = b(new JSONObject(string));
                if (b != null) {
                    this.b = b;
                }
                stringBuilder = new StringBuilder();
                stringBuilder.append("loadLocalConfig: ");
                stringBuilder.append(b == null ? "null" : b.toString());
                b.a(str, stringBuilder.toString());
            } catch (Throwable th) {
                th.printStackTrace();
                stringBuilder = new StringBuilder();
                stringBuilder.append("loadLocalConfig: except: ");
                stringBuilder.append(th.getMessage());
                b.a(str, stringBuilder.toString());
            }
        }
    }

    public final d b(JSONObject jSONObject) {
        String str = "host_replace_map";
        String str2 = "local_host_filter";
        String str3 = "probe_enable";
        String str4 = "local_enable";
        try {
            String string;
            d dVar = new d();
            boolean z = true;
            if (jSONObject.has(str4)) {
                dVar.a = jSONObject.getInt(str4) != 0;
            }
            if (jSONObject.has(str3)) {
                if (jSONObject.getInt(str3) == 0) {
                    z = false;
                }
                dVar.b = z;
            }
            if (jSONObject.has(str2)) {
                JSONArray jSONArray = jSONObject.getJSONArray(str2);
                HashMap hashMap = new HashMap();
                if (jSONArray.length() > 0) {
                    for (int i = 0; i < jSONArray.length(); i++) {
                        string = jSONArray.getString(i);
                        if (!TextUtils.isEmpty(string)) {
                            hashMap.put(string, Integer.valueOf(0));
                        }
                    }
                }
                dVar.c = hashMap;
            } else {
                dVar.c = null;
            }
            if (jSONObject.has(str)) {
                JSONObject jSONObject2 = jSONObject.getJSONObject(str);
                HashMap hashMap2 = new HashMap();
                if (jSONObject2.length() > 0) {
                    Iterator keys = jSONObject2.keys();
                    while (keys.hasNext()) {
                        str4 = (String) keys.next();
                        string = jSONObject2.getString(str4);
                        if (!TextUtils.isEmpty(str4)) {
                            if (!TextUtils.isEmpty(string)) {
                                hashMap2.put(str4, string);
                            }
                        }
                    }
                }
                dVar.d = hashMap2;
            } else {
                dVar.d = null;
            }
            dVar.e = jSONObject.optInt("req_to_cnt", dVar.e);
            dVar.f = jSONObject.optInt("req_to_api_cnt", dVar.f);
            dVar.g = jSONObject.optInt("req_to_ip_cnt", dVar.g);
            dVar.h = jSONObject.optInt("req_err_cnt", dVar.h);
            dVar.i = jSONObject.optInt("req_err_api_cnt", dVar.i);
            dVar.j = jSONObject.optInt("req_err_ip_cnt", dVar.j);
            dVar.k = jSONObject.optInt("update_interval", dVar.k);
            dVar.l = jSONObject.optInt("update_random_range", dVar.l);
            dVar.m = jSONObject.optString("http_code_black", dVar.m);
            return dVar;
        } catch (Throwable th) {
            th.printStackTrace();
            return null;
        }
    }

    public void c() {
        String str = "TNCConfigHandler";
        StringBuilder stringBuilder;
        try {
            Object a;
            CharSequence charSequence = "";
            try {
                a = c.a().a(this.a, "tnc_config", charSequence);
            } catch (Exception unused) {
                a = charSequence;
            }
            if (a instanceof String) {
                charSequence = String.valueOf(a);
            }
            if (TextUtils.isEmpty(charSequence)) {
                b.a(str, "loadLocalConfigForOtherProcess, data empty");
                return;
            }
            String str2;
            d b = b(new JSONObject(charSequence));
            stringBuilder = new StringBuilder();
            stringBuilder.append("loadLocalConfigForOtherProcess, config: ");
            if (b == null) {
                str2 = "null";
            } else {
                str2 = b.toString();
            }
            stringBuilder.append(str2);
            b.a(str, stringBuilder.toString());
            if (b != null) {
                this.b = b;
            }
        } catch (Throwable th) {
            stringBuilder = a.g("loadLocalConfigForOtherProcess, except: ");
            stringBuilder.append(th.getMessage());
            b.a(str, stringBuilder.toString());
        }
    }
}
